/**
 *
 */
package com.elcom.emocbc.cbm;

import java.util.Date;
import java.util.concurrent.SynchronousQueue;

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;

import com.elcom.emocbc.util.ActionThread;
import com.elcom.emocbc.util.GeneratorSeq;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author tiamo114
 *
 */
public class BroadcastUnit extends ActionThread {

    private final Logger LOG = LoggerFactory.getLogger(BroadcastUnit.class);
    private SynchronousQueue<BroadcastDTO> q = null;
    private Scheduler sched = null;

    /**
     * @param name
     */
    public BroadcastUnit(String name, SynchronousQueue<BroadcastDTO> _q, Scheduler sch) {
        super(name);
        this.q = _q;
        this.sched = sch;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#action()
     */
    @Override
    protected void action() throws Exception {
        BroadcastDTO broadcast = this.q.take();

        LOG.info("------- Scheduling Jobs -------------------");

        int seqID = GeneratorSeq.getNextSeq();
        // Establish Job
        JobDetail job = new JobDetail("BroadcastJob" + seqID, "CellBroadcastJobs" + seqID, BroadcastJob.class);
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("broadcast", broadcast);
        jobDataMap.put("schedule", sched);
        jobDataMap.put("job_name", job.getName());
        jobDataMap.put("job_group", job.getGroup());
        job.setJobDataMap(jobDataMap);

        DateTime now = DateTime.now().plusSeconds(15);
        Date startDate;
        Date endDate;
        if(now.isAfter(broadcast.getStartDate().getTime())){
            LOG.debug("Start date of broadcast has passed: {}", broadcast.getStartDate());
            startDate = now.toDate();
            endDate = now.plusMinutes(1).toDate();
        } else {
            startDate = broadcast.getStartDate();
            endDate = broadcast.getEndDate();
        }
        
        SimpleTrigger trigger = new SimpleTrigger("BroadcastTrigger" + seqID, "CellBroadcastJobs" + seqID, startDate, endDate,
                SimpleTrigger.REPEAT_INDEFINITELY, 60000L);

        Date ft = sched.scheduleJob(job, trigger);
        LOG.info("{} will run at: {} and repeat: {} times, every {} seconds", job.getFullName(), ft, trigger.getRepeatCount(), trigger.getRepeatInterval() / 1000);
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.elcom.livescreen.util.ActionThread#onException(java.lang.Exception)
     */
    @Override
    protected void onException(Exception e) {
        LOG.error("{} is onException: ", this.getName(), e);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#onExecuting()
     */
    @Override
    protected void onExecuting() throws Exception {
        LOG.info("{} is onExecuting", this.getName());
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#onKilling()
     */
    @Override
    protected void onKilling() {
        LOG.info("{} is onKilling", this.getName());
    }

    /*
     * (non-Javadoc)
     *
     * @see com.elcom.livescreen.util.ActionThread#sleeptime()
     */
    @Override
    protected long sleeptime() throws Exception {
        return 1;
    }
}
